package components
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.ProgressBar;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class Resource{
		private var panel:SuperPanel = new SuperPanel();
		private var sifraLabel:Label = new Label();
		private var sifraInput:TextInput = new TextInput();
		private var btnSifra:Button = new Button();
		private var tool:Tool = new Tool();
		private var _id:String = "";
		private var _url:String = "";
		private var _tip:String = "";
		private var _user:String = "";
		private var _username:String = "";
		private var _program:String = "";
		private var _sifra:String = "";
		private var _naziv:String = "";
		private var _opis:String = "";
		private var _datum:String = "";
		private var _vrijeme:String = "";
		
		public var  external:Boolean = false;
		public var  rapidshare:Boolean = false;
		
		private var labelNaziv:Label = new Label();
		private var labelOpis:Label = new Label();
		private var textOpis:TextArea = new TextArea();
		private var labelUsername:Label = new Label();
		private var labelProgram:Label = new Label();
		private var labelDatum:Label = new Label();
		private var labelVrijeme:Label = new Label();
		private var btnDownload:Button = new Button();
		private var btnCancel:Button = new Button();
		private var btnOtvori:Button = new Button();
		private var ikona:Image = new Image();
		private var fileToDownload:FileReference = new FileReference();
		private var pb:ProgressBar = new ProgressBar();

		public function Resource(p:SuperPanel){
			panel = p;
			sifraLabel.x = 50;
			sifraLabel.y = 116;
			sifraLabel.text = "Unesite sifru resursa: ";
			sifraInput.x = 180;
			sifraInput.y = 115;
			sifraInput.width = 80;
			sifraInput.displayAsPassword = true;
			btnSifra.x = 50;
			btnSifra.y = 140;
			btnSifra.width = 210;
			btnSifra.label = "OK";
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
		}
		
		public function onResize(e:ResizeEvent):void{
			textOpis.width = panel.width - 30;
			btnDownload.width = ((panel.width -30)/2)-10;
			btnOtvori.width = ((panel.width -30)/2);
			ikona.x = panel.width - 90;
			btnOtvori.x = 10+(panel.width -30)/2;
			btnOtvori.width = ((panel.width -30)/2);
			btnDownload.width = ((panel.width -30)/2)-10;
			btnCancel.width = ((panel.width -30)/2)-10;
			btnOtvori.y = panel.height -55;
			btnDownload.y = panel.height -55;
			btnCancel.y = panel.height -55;
			textOpis.height = panel.height - 210;
			pb.y = panel.height - 90;
			pb.width = panel.width - 30;
		}
		
		public function show(id:String, url:String, tip:String, user:String, username:String, program:String, sifra:String, naziv:String, opis:String, datum:String, vrijeme:String):void{	
			_id = id;
			_url = url;
			_tip = tip;
			_user = user;
			_username = username;
			_program = program;
			_sifra = sifra;
			_naziv = naziv;
			_opis = opis;
			_datum = datum;
			_vrijeme = vrijeme;
		}
		
		public function set sifrirano(x:Boolean):void{
			if(x) {
				panel.addChild(sifraLabel);
				panel.addChild(sifraInput);
				panel.addChild(btnSifra);
				btnSifra.addEventListener(MouseEvent.CLICK, onSifraEntered)
			} else draw();
		}
		
		public function onSifraEntered(e:Event):void{
			if(_sifra==sifraInput.text){
				draw();
				panel.removeChild(sifraLabel);
				panel.removeChild(sifraInput);
				panel.removeChild(btnSifra);
			} else tool.createAlert("Greska", "Neispravna sifra!");
		}
		
		public function draw():void{
			labelNaziv.x = 10;
			labelNaziv.y = 27;
			labelNaziv.htmlText = "Naziv:    <b>"+_naziv.substring(0,25)+"..."+"</b>";
			labelUsername.x = 10;
			labelUsername.y = 57;
			labelUsername.htmlText = "Autor:     <b>"+_username+"</b>";
			labelProgram.x = 10;
			labelProgram.y = 42;
			labelProgram.htmlText = "Tip:      <b>  "+_program+"</b>";
			labelDatum.x = 10;
			labelDatum.y = 72;
			labelDatum.htmlText = "Datum:  <b> "+_datum+"</b>";
			labelVrijeme.x = 10;
			labelVrijeme.y = 87;
			labelVrijeme.htmlText = "Vrijeme: <b>"+_vrijeme+"</b>";

			labelOpis.x = 10;
			labelOpis.y = 102;
			labelOpis.htmlText = "Opis resursa: ";
			textOpis.x = 10;
			textOpis.y = 117;
			textOpis.width = panel.width - 30;
			textOpis.height = panel.height - 210;
			textOpis.editable = false;
			textOpis.htmlText = _opis;
			pb.x = 10;
			pb.y = panel.height - 90;
			pb.width = panel.width - 30;
			pb.height = 5;
			pb.mode = "manual";
			btnDownload.x = 10;
			btnDownload.height = 35;
			btnDownload.y = panel.height -55;
			btnDownload.width = ((panel.width -30)/2)-10;
			btnDownload.label = "Preuzmi resurs";
			btnCancel.x = 10;
			btnCancel.height = 35;
			btnCancel.y = panel.height -55;
			btnCancel.width = ((panel.width -30)/2)-10;
			btnCancel.label = "Otkazi skidanje";
			btnOtvori.x = 10+(panel.width -30)/2;
			btnOtvori.y = panel.height -55;
			btnOtvori.width = ((panel.width -30)/2);
			btnOtvori.height = 35;
			btnOtvori.label = "Otvori resurs";
			switch(_tip){
				case "pdf":
					ikona.source = panel.parentApplication.bigPDF;
					break;
				case "html":
					ikona.source = panel.parentApplication.bigHTML;
					break;
				case "slt":
					ikona.source = panel.parentApplication.bigSLT;
					break;
				case "jpg":
					ikona.source = panel.parentApplication.MATERIJAL_PATH+_url;
					break;
				case "gif":
					ikona.source = panel.parentApplication.MATERIJAL_PATH+_url;
					break;
				case "png":
					ikona.source = panel.parentApplication.MATERIJAL_PATH+_url;
					break;
				case "RAP":
					ikona.source = panel.parentApplication.bigRapidshare;
					break;
			}
			btnOtvori.enabled = tool.canOpen(_tip);
			btnDownload.enabled = tool.canDownload(_tip);
			ikona.x = panel.width - 90;
			ikona.y = 27;
			ikona.height = 70;
			ikona.width = 70;
			panel.addChild(labelNaziv);
			panel.addChild(labelOpis);
			panel.addChild(labelProgram);
			panel.addChild(labelUsername);
			panel.addChild(labelDatum);
			panel.addChild(labelVrijeme);
			panel.addChild(textOpis);
			panel.addChild(btnDownload);
			panel.addChild(btnOtvori);
			panel.addChild(ikona);
			
			btnDownload.addEventListener(MouseEvent.CLICK, onDownload);
			btnOtvori.addEventListener(MouseEvent.CLICK, onOtvori);
			btnCancel.addEventListener(MouseEvent.CLICK, cancelDownload);
			labelUsername.buttonMode = true;
			labelUsername.useHandCursor = true;
			labelUsername.mouseChildren = false;
			labelUsername.addEventListener(MouseEvent.CLICK, onClickLabel);
		}
		
		public function onClickLabel(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("KORISNIK - "+_user)){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("KORISNIK - "+_user, 0, 330, 220, 250, 120, true, false, "KORISNIK - "+_user));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var user:User = new User(s);
				user.id = _user;
			}
		}
		
		public function onDownload(e:MouseEvent):void{
			if(external){
				if(rapidshare){
					fileToDownload.download(new URLRequest(_url), tool.uniqueNumber());
				} else {
					fileToDownload.download(new URLRequest(_url), tool.uniqueNumber());
				}
			} else {
				fileToDownload.download(new URLRequest(panel.parentApplication.MATERIJAL_PATH+_url), _naziv+"."+_tip);
			}
			fileToDownload.addEventListener(Event.OPEN, openHandler);
        	fileToDownload.addEventListener(ProgressEvent.PROGRESS, progressHandler);
	        fileToDownload.addEventListener(Event.COMPLETE, completeHandler);
	        fileToDownload.addEventListener(IOErrorEvent.IO_ERROR, onError);
		}
		
		public function onError(e:IOErrorEvent):void{
			tool.createAlert("Greska!", "Dogodila se greska prilikom preuzimanja fajla.");
		}
		
		private function openHandler(event:Event):void{
			panel.removeChild(btnDownload);
			panel.addChild(btnCancel);
			try{
				panel.removeChild(pb);
			} catch(e:Error){
				//
			}
			panel.addChild(pb);
        	pb.label = "PREUZETO %3%%";
    	}
    	
		private function progressHandler(event:ProgressEvent):void{
        	pb.setProgress(event.bytesLoaded, event.bytesTotal);
    	}
    	
		private function completeHandler(event:Event):void{
        	pb.label = "PREUZIMANJE ZAVRSENO";
			panel.removeChild(btnCancel);
			panel.addChild(btnDownload);
    	}
    	
    	public function cancelDownload(event:MouseEvent):void{
        	fileToDownload.cancel();
        	pb.label = "PREUZIMANJE OTKAZANO";
			panel.removeChild(btnCancel);
			panel.addChild(btnDownload);
    	}

		public function onOtvori(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist(_naziv+"("+_id+")")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel(_naziv+"("+_id+")", 0, 400, 400, 300, 0, true, true, _naziv+"("+_id+")"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var web:loadHTMLPage = new loadHTMLPage(s);
				switch(_tip){
					case "pdf":
						var pdf:loadPdfPage = new loadPdfPage(s);
	 					pdf.loadPDF(panel.parentApplication.MATERIJAL_PATH+_url);
						break;
					case "html":
						web = new loadHTMLPage(s);
	 					web.loadHTML(_url);
	 					web.load();
						break;
					case "slt":
						web = new loadHTMLPage(s);
	 					web.loadHTMLTekst(_url);
	 					web.loadTekst();
						break;
				}
			}
		}
	}
}